$(function () {

    // 倒计时
    let $time = $('.time')
    function fn() {
        let data = new Date()
        let hour = data.getHours()

        let year = data.getFullYear()
        let month = data.getMonth()
        let day = data.getDate()
        // 开启倒计时，假设每天19点截止，超过19点则计算距离第二天的时间
        if (hour >= 19) {
            day += 1
        }
        let dist = new Date(year, month, day, 19)

        let stamp = dist - data

        stamp /= 1000
        // 转成时
        let h = parseInt(stamp / 60 / 60 % 24)
        // 算出分
        let min = parseInt(stamp / 60 % 60)
        // 算出秒
        let sec = parseInt(stamp % 60)

        $time.html(`申请时间剩余：${h}小时${min}分钟${sec}秒`)
    }
    fn()
    setInterval(fn, 1000);

    let hot = document.querySelector('.hot-main');
    let ul = document.querySelector('.lunbo');
    let prev = document.querySelector('.prev')
    let next = document.querySelector('.next');
    let con = document.querySelector('.hot-bd');


    con.onmouseenter = function () {
        clearInterval(timer)
    }
    con.onmouseleave = function () {
        timer = setInterval(function () {
            prev.click();
        }, 2000)
    }

    let num = 0;
    // 克隆
    let first = ul.children[0].cloneNode(true);
    ul.appendChild(first);

    console.log(ul.children.length);
    prev.onclick = function () {

        if (num == ul.children.length - 1) {
            num = 0;
            ul.style.left = 0;
        }
        num++;
        animer(ul, -num * hot.offsetWidth);
    }

    next.onclick = function () {

        if (num == 0) {
            num = ul.children.length - 1;
            ul.style.left = -num * hot.offsetWidth + 'px';
        }
        num--;
        animer(ul, -num * hot.offsetWidth);
    }

    timer = setInterval(function () {
        prev.click();
    }, 2000)

    // 返回顶部
    $(document).scroll(function () {
        if ($(this).scrollTop() >= 1000) {
            $('.span').css({
                position: 'fixed',
                left: 1400,
                top: 500
            })
            $('.span').stop(true).fadeIn(300);
        } else {
            $('.span').stop(true).fadeOut(300);
        }
    })

    $('.span').click(function () {
        $('html,body').animate({
            scrollTop: 0
        }, 300)
    })

    // 放大镜
    $('.sear').click(function () {
        $('.search-input').addClass('show');
    })
    $('.dianji').click(function () {

        $('.more').attr('src', './images/loading-icon.gif')


        setTimeout(function () {
            // 假设这是服务器返回的数据
            let data = [
                { "images": "images/kuwan01.jpg", "text": "索尼 FDR-AXP55 摄像机", "price": "￥499" },
                { "images": "images/kuwan01.jpg", "text": "索尼 FDR-AXP55 摄像机", "price": "￥498" },
                { "images": "images/kuwan01.jpg", "text": "索尼 FDR-AXP55 摄像机", "price": "￥497" },
                { "images": "images/kuwan01.jpg", "text": "索尼 FDR-AXP55 摄像机", "price": "￥496" },
                { "images": "images/kuwan01.jpg", "text": "索尼 FDR-AXP55 摄像机", "price": "￥495" },
                { "images": "images/kuwan01.jpg", "text": "索尼 FDR-AXP55 摄像机", "price": "￥494" },
                { "images": "images/kuwan01.jpg", "text": "索尼 FDR-AXP55 摄像机", "price": "￥493" },
                { "images": "images/kuwan01.jpg", "text": "索尼 FDR-AXP55 摄像机", "price": "￥492" }
            ]

            let = $('.foo')

            for (let i = 0; i < data.length; i++) {
                $(`<li >
            <img src="${data[i].images}" width="220" height="130"/>
                <div class = "tit"><h4>${data[i].text}</h4>
                <span class="hong">${data[i].price}</span>
                <div class="dianzan">
                <span><img src="./images/xin.png" alt=""> </span>
                <span>3</span>
                <span><img src="./images/zan.png" alt=""> </span>
                <span>3</span>
            </div>
                </div>
                </li>`).appendTo('.foo')
            }

            $('.dianji').text('点击加载更多')
            $('.more').attr('src', './images/more.png')

        }, 1000)
    })

    let xin = 0;
    $("body").on("click", ".xin", function () {
        xin++
        // $(this).parent().next().text(xin)
        $(this).text(parseInt($(this).parent().next().text(xin)));
    });
})